package xyz.kongbai121.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.kongbai121.demo.domain.Article;
import xyz.kongbai121.demo.domain.Comment;
import xyz.kongbai121.demo.domain.User;
import xyz.kongbai121.demo.mapper.ArticleMapper;
import xyz.kongbai121.demo.mapper.CommentMapper;
import xyz.kongbai121.demo.mapper.UserMapper;
import xyz.kongbai121.demo.service.ICommentService;
import xyz.kongbai121.demo.util.CommentUtil;
import xyz.kongbai121.demo.util.UserPowerException;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
@Transactional
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String save(String content, Long articleId, Long commentId, Long toUserId, String token) {
        String openid = redisTemplate.opsForValue().get(token);
        Integer state = userMapper.getState(openid);
        Article article = articleMapper.get(articleId);
        User toUser = null;
        //最后存储对象
        Comment comment = new Comment();
        //TODO 如果有父评论
        if (commentId != null && commentId > 0 && toUserId > 0){
            toUser = userMapper.selectById(toUserId);
            if (commentMapper.hasComment(commentId) <= 0 || toUser == null){
                throw new UserPowerException("非法操作");
            }
            //TODO 如果父评论不是自己,用户未读评论加一
            if(!article.getOpenid().equals(openid)) userMapper.addUnreadComment(toUser.getOpenid());
            //TODO 上级评论用户id
            comment.setToOpenid(toUser.getOpenid());
        }else {
            commentId = null;
            toUserId = null;
        }
        //判断是否有操作权限和文章是否存在
        if (state == 1 && article != null) {
            //TODO 添加评论
            comment.setContent(content);
            comment.setOpenid(openid);
            comment.setArticleId(articleId);
            comment.setCommentId(commentId);
            comment.setArticleHead(article.getHead());
            comment.setDeliverTime(new Date());
            comment.setAuthorOpenid(article.getOpenid());
            comment.setIsRead(0);
            commentMapper.insert(comment);
            //TODO 如果不是自己评论自己,用户未读评论加一
            if(!article.getOpenid().equals(openid)) userMapper.addUnreadComment(article.getOpenid());
            //TODO 文章评论加1
            redisTemplate.boundValueOps("articleCommentNum"+articleId).increment(1);
            return "评论成功";
        }else {
            throw new UserPowerException("没有权限");
        }
    }

    @Override
    public String delete(Long id, String token) {
        String openid = redisTemplate.opsForValue().get(token);
        Comment comment = commentMapper.selectById(id);
        //判断评论是否是当前用户发的或者用户是管理员
        if (comment.getOpenid() != null && comment.getOpenid().equals(openid) || userMapper.getAdmin(openid) == 1){
            //TODO 删除评论
            commentMapper.deleteById(id);
            //TODO 删除子评论
            UpdateWrapper<Comment> wrapper = new UpdateWrapper<>();
            wrapper.eq("comment_id",id);
            commentMapper.delete(wrapper);
            //TODO 文章评论减1
            redisTemplate.boundValueOps("articleCommentNum"+comment.getArticleId()).increment(-1);
            //TODO 如果评论未读,未读数减1
            if (comment.getIsRead() == 0) userMapper.subUnreadComment(comment.getAuthorOpenid());
            return "操作成功";
        }else {
            throw new UserPowerException("没有权限");
        }
    }

    @Override
    public List<Comment> list(Long articleId) {
        //TODO  查询文章所有的评论
        List<Comment> list = commentMapper.list(articleId);
        //TODO  处理父子评论关系
        CopyOnWriteArrayList<Comment> comments = new CopyOnWriteArrayList<>(list);
        return CommentUtil.commentLink(comments);
    }
}
